package cn.itcast.web.controller.company;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.vo.PageBean;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/company",name = "企业管理")

public class CompanyController extends BaseController {

    //依赖注入
    @Autowired
    private CompanyService companyService;



    //分页查询
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
       /* //获取方法
        List<Company> list = companyService.findAll();
        //存入域中
        request.setAttribute("list",list);*/
        /*PageBean pageBean = companyService.findAll(pageNum, pageSize);
        request.setAttribute("page",pageBean);*/

        PageInfo page = companyService.findByPageHelper(pageNum, pageSize);
        request.setAttribute("page",page);

        return "company/company-list";

    }


    //跳转到新增页面
    @RequestMapping(value = "/toAdd",name = "新增页面")
    public String toAdd(){
        //重定向到添加页面
        return "company/company-add";
    }

    //添加方法
    @RequestMapping(value = "/edit",name = "添加/修改方法")
        public String edit(Company company){
            //判断
            if (StrUtil.hasEmpty(company.getId())){
                //为空,新增
                //调用方法
                companyService.save(company);
            }else{
                //修改
                companyService.update(company);
            }
            //重定向
            return "redirect:/company/list.do";
    }

    //修改页面
    @RequestMapping(value = "/toUpdate",name = "修改页面")
    public String toUpdate(String id){
        //获取通过id查询方法
        Company byId = companyService.findById(id);
        request.setAttribute("company",byId);
        //返回
        return "company/company-update";
    }

    //删除
    @RequestMapping(value = "/delete",name = "删除方法")
    public String delete(String id){
        //获取方法
        companyService.delete(id);
        //返回
        return "redirect:/company/list.do";
    }




}
